package com.puman.watchtrade.fragment.set;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebSettings.TextSize;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.puman.watchtrade.MainActivity;
import com.puman.watchtrade.R;
import com.puman.watchtrade.util.Config;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint({ "ValidFragment", "NewApi" })
public class AboutFragment extends Fragment {
	private View mView;

	/*
	 * UI
	 */
	private WebView newsWebview;
	private TextView topTitle;
	/*
	 * variables
	 */
	private String flag = "0";

	private static final String APP_CACAHE_DIRNAME = "/webcache";
	private String Url = "";

	public AboutFragment() {
		super();
	}

	/*
	 * flag 1：用户协议 2：商家协议 3：关余麦拉
	 */
	public AboutFragment(String flag) {
		this.flag = flag;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.about_layout, null);
		initView();
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		// 设置动画执行的时间
		alphaAnimation.setDuration(1500);
		AnimationSet animationalpha = new AnimationSet(true);

		animationalpha.addAnimation(alphaAnimation);
		mView.findViewById(R.id.share_webview).startAnimation(animationalpha);
		return mView;
	}

	private void initView() {

		topTitle = (TextView) mView.findViewById(R.id.top_title);
		if (flag.equals("1")) {
			topTitle.setText("用户协议");
			mView.findViewById(R.id.main_left_img).setVisibility(View.GONE);
			mView.findViewById(R.id.sold_info).setVisibility(View.VISIBLE);
			mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							MainActivity.getInstant().fragmentManager
									.popBackStack();
						}
					});
			Url = Config.law;
		} else if (flag.equals("2")) {
			topTitle.setText("会员协议");
			mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							MainActivity.getInstant().fragmentManager
									.popBackStack();
						}
					});
			Url = Config.merchantsDeal;
		} else if (flag.equals("3")) {
			topTitle.setText("关于会麦");
			mView.findViewById(R.id.main_left_imgbtn).setOnClickListener(
					MainActivity.getInstant().mySetOnClickListener);
			Url = Config.about;
		}
		newsWebview = (WebView) mView.findViewById(R.id.share_webview);
		// newsWebview = new WebView(MainActivity.getInstant()
		// .getApplicationContext());
		// LinearLayout mll = (LinearLayout)
		// mView.findViewById(R.id.web_layout);
		// mll.addView(newsWebview);

//		newsWebview.getSettings().setJavaScriptEnabled(true);
//		newsWebview.getSettings().setLoadWithOverviewMode(true);
		WebSettings webSettings = newsWebview.getSettings();
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

		newsWebview.getSettings().setSupportZoom(false);
//		newsWebview.getSettings().setTextSize(WebSettings.TextSize.LARGER);
		newsWebview.getSettings().setBuiltInZoomControls(false);
		newsWebview.getSettings().setUseWideViewPort(false);
		newsWebview.getSettings().setAllowFileAccess(true);
		newsWebview.getSettings().setPluginsEnabled(true);

		newsWebview.getSettings().setRenderPriority(RenderPriority.HIGH);
		newsWebview.getSettings().setCacheMode(
				WebSettings.LOAD_CACHE_ELSE_NETWORK); // ���� ����ģʽ
		newsWebview.getSettings().setDomStorageEnabled(true);
		newsWebview.getSettings().setDatabaseEnabled(true);
		String cacheDirPath = MainActivity.getInstant().getFilesDir()
				.getAbsolutePath()
				+ APP_CACAHE_DIRNAME;
		newsWebview.getSettings().setDatabasePath(cacheDirPath);
		newsWebview.clearCache(true);
		newsWebview.getSettings().setAppCachePath(cacheDirPath);
		newsWebview.getSettings().setAppCacheEnabled(true);

		// newsWebview.setBarHeight(3);
		newsWebview.setWebViewClient(new WebViewClient() {

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		newsWebview.setWebChromeClient(new WebChromeClient() {

			// ��չ������ϴ��ļ�
			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType, String capture) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, ""),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);

			}

			// 3.0 + �����������
			public void openFileChooser(ValueCallback<Uri> uploadMsg,
					String acceptType) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);
			}

			// Android < 3.0 �����������
			public void openFileChooser(ValueCallback<Uri> uploadMsg) {
				MainActivity.getInstant().mUploadMessage = uploadMsg;
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.addCategory(Intent.CATEGORY_OPENABLE);
				intent.setType("image/*");
				getActivity().startActivityForResult(
						Intent.createChooser(intent, "��ɲ�����Ҫʹ��"),
						MainActivity.getInstant().FILECHOOSER_RESULTCODE);

			}

		});
		showWeb();
		// Log.i("curry",
		// "isHardwareAccelerated:"+newsWebview.isHardwareAccelerated());
	}

	public void showWeb() {
//		String URL = Config.baseUrlStr + "watchtrade/html/disclaimer.html";
		newsWebview.loadUrl(Url);

	}

}
